/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.cash.service;

import java.util.Date;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.cash.dao.StoreReserveDao;
import com.chlitina.store.modules.cash.entity.StoreReserve;

/**
 * 预约登记Service
 * @author lvbo
 * @version 2013-11-05
 */
@Component
@Transactional(readOnly = true)
public class StoreReserveService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreReserveService.class);
	
	@Autowired
	private StoreReserveDao storeReserveDao;
	
	public StoreReserve get(Long id) {
		return storeReserveDao.findOne(id);
	}
	
	public Page<StoreReserve> find(Page<StoreReserve> page, StoreReserve storeReserve,String startDate, String endDate) {
		DetachedCriteria dc = storeReserveDao.createDetachedCriteria();
		if (storeReserve.getStore()!=null && storeReserve.getStore().getId()>0){
			dc.add(Restrictions.eq("store.id", storeReserve.getStore().getId()));
		}
		Date start = null;
		Date end = null;
		try {
			start = DateUtils.parseDate(startDate);
			start = DateUtils.getBeginOfDay(start);
			end = DateUtils.parseDate(endDate);
			end = DateUtils.getEndOfDay(end);
		} catch (Exception e) {
//			System.out.println("字符串转换日期异常");
		}
		if (start != null && end == null) {
			dc.add(Restrictions.ge("reserveTime", start));
		} else if (start == null && end != null) {
			dc.add(Restrictions.le("reserveTime", end));
		} else if (start != null && end != null) {
			dc.add(Restrictions.between("reserveTime", start, end));
		}
		dc.addOrder(Order.desc("id"));
		return storeReserveDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreReserve storeReserve) {
		storeReserveDao.clear();
		storeReserveDao.save(storeReserve);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		storeReserveDao.delete(id);
	}
	
}
